package project.bk;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.provider.Settings.System;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

	private static final String TAG = "ERROR";
	private SurfaceHolder mHolder;
	private Camera mCamera;
	private MediaRecorder mVideo;

	public CameraPreview(Context context, Camera camera) {
		super(context);
		mCamera = camera;

		// Install a SurfaceHolder.Callback so we get notified when the
		// underlying surface is created and destroyed.

		mHolder = getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// The Surface has been created, now tell the camera where to draw the preview.
		try {
			mCamera.setPreviewDisplay(holder);
			mCamera.startPreview();
		} catch (IOException e) {
			Log.d(TAG, "Error setting camera preview: " + e.getMessage());
		}
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
	}

	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
			int arg3) {			
	}




	//initialize Video
	public void initVideo(){
		mVideo.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
		mVideo.setVideoSource(MediaRecorder.VideoSource.CAMERA);
		CamcorderProfile camCorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
		mVideo.setProfile(camCorderProfile);
		mVideo.setOutputFile("/sdcard/DCIM/myvide_"+System.DATE_FORMAT+".3gp");
		mVideo.setMaxDuration(60000);
		mVideo.setMaxFileSize(500000000);
	}

	//start Video
	public void prepareVideo(){
		mVideo.setPreviewDisplay(mHolder.getSurface());
		try {
			mVideo.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



}
